<?php
error_reporting(E_ALL);

// ID: $Id: $

/**
 * User Class.
 *
 *
 */


class User
{
    private $_username;
    private $_row;

    // the following are queried when needed

    /**
     * Class Initializer
     *
     */

    // initializer
    // public function User($username)
    function __construct($username)
    {
        if(empty($username)) {
            $this->_username = USER_ANONYMOUS ;
        } 
        else {
            $this->_username = $username ;
        }

        $this->Init();
    }

    private function Init()
    {
        global $DpDb;

        $username = $this->_username;
        if( !strcmp( $username, USER_ANONYMOUS ) ) {
            return ;
        }
        $sql = "SELECT
                    u.username,
                    CASE WHEN EXISTS 
                    (   SELECT 1 FROM pagetasks AS pt
                        WHERE username = u.username
                    ) THEN 1 ELSE 0 END AS ismyprojects,
                    CASE WHEN EXISTS
                    (   SELECT 1 FROM userquals
                        WHERE username = u.username
                            AND qualcode = 'PM'
                    ) THEN 1 ELSE 0 END AS isprojectmanager
                FROM
                    users AS u
                WHERE 
                    u.username = '$username'";

        $this->_row = $DpDb->SqlOneRow($sql);
        if(empty($this->_row)) {
            $this->_username = USER_ANONYMOUS;
            return;
        }

        $settings = $this->UserSettings() ;
        foreach( $settings as $s ) {
            $this->_row[$row['setting']] = $row['value'];
        }
    }

    public function IsLoggedIn()
    {
        return strcmp( $this->_username, USER_ANONYMOUS ) ;
    }

    // Can log out here, or with $Context directly.
    public function LogOut()
    {
    	global $DpDb;
    	
        // $User->SetLoggedOut();
        $sql = "DELETE FROM sessions
                WHERE sid = '".session_id()."'";
        $DpDb->SqlExecute($sql);
        $_SESSION['username'] = USER_ANONYMOUS ;
        $this->_username = USER_ANONYMOUS ;
        ini_set('session.bug_compat_warn', 0 );
        session_write_close();
    }

    /**
     * Set the Real Name of the User
     *
     * - returns true if successful at setting user's real name
     * - returns false otherwise
     */

    public function SetRealName($real_name)
    {
        $this->_setSetting('real_name', $real_name);
    }

    /**
     * Real Name of User
     */

    public function RealName()
    {
        return $this->_row['real_name'];
    }

    public function UserPmProjects()
    {
        global $DpDb;

        $sql = "SELECT
                    p.projectid,
                    p.state,
                    pt.taskcode,
                        SUM(CASE WHEN pt.checkouttime IS NULL THEN 1 ELSE 0 END)
                            AS virgincount,
                        SUM(CASE WHEN pt.checkouttime IS NOT NULL 
                                    AND pt.completiontime IS NULL THEN 1 ELSE 0 END) 
                            AS inprogresscount,
                        SUM(CASE WHEN pt.completiontime IS NOT NULL THEN 1 ELSE 0 END) 
                            AS completedcount,
                        (
                            SELECT COUNT(1) FROM pages 
                            WHERE projectid = pt.projectid
                        ) AS pagecount
                FROM projects AS p

                JOIN pagetasks pt
                    ON p.projectid = pt.projectid

                WHERE 
                    p.username = '$username'
                    AND p.state = 'A'
                GROUP BY
                    p.projectid,
                    p.state,
                    pt.taskcode";
        $projs = $DpDb->SqlObjects($sql) ;
        return $projs;
    }

    public function IsMyProjects()
    {
        return $this->_row['ismyprojects'] ;
    }
    /**
     * Returns the username provided at creation of User object
     */

    public function UserName()
    {
        return $this->_row['username'];
    }

    public function InterfaceLanguage()
    {
        return $this->Property('u_intlang');
    }
    /**
     * Return the number of days between the dates on
     * which two date values occurred, removing funny
     * stuff for earliness or lateness within day of each.
     */

    private function _daysBetween($earlierdate, $laterdate)
    {
        return ($laterdate - $earlierdate) / 24 / 60 / 60 ;
    }

    public function IsProjectManager()
    {
        return $this->_row['isprojectmanager'] ;
    }

    public function MyTasksForProject($projectid)
    {

        $sql = "SELECT
            taskcode,
            pagecode,
            checkouttime,
            completiontime,
            CAST(
                    (UNIX_TIMESTAMP() 
                     - COALESCE(completiontime, checkouttime))
                    / 60 / 60 / 24 AS SIGNED
                ) AS days_ago
                FROM
                pagetasks
                WHERE
                projectid = '$projectid'
                AND username = '$this->_username'
                ORDER BY
                COALESCE(completiontime, checkouttime) DESC";

        return $DpDb->SqlRows($sql);
    }

    public function MyPages()
    {
        global $DpDb ;

        $username = $this->Username() ;
        $sql = "SELECT DISTINCT
                    pt.projectid,
                    pt.pagecode,
                    pt.taskcode,
                    p.nameofwork,
                    t.sequencenumber,
                    1 AS pages_active,
                    1 AS diffcount,
                    1 AS pages_mine,
                    UNIX_TIMESTAMP() AS max_round_timestamp,
                    UNIX_TIMESTAMP() AS date_diffable
                FROM
                    pagetasks AS pt
                JOIN
                    projects AS p
                ON
                    pt.projectid = p.projectid
                JOIN
                    tasks AS t
                ON
                    pt.taskcode = t.taskcode
                WHERE
                    pt.username = '$username'";
        return $DpDb->SqlObjects($sql) ;
    }

    private function UserSettings()
    {
        global $DpDb;

        $sql = "SELECT * FROM usersettings
                WHERE username = '%s'";
        return $DpDb->SqlRows($sql) ;
    }
}
?>
